package com.weme.home.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.viewpager2.widget.ViewPager2
import com.purepulse.home.R
import com.weme.common.util.dp2px
import com.weme.common.view.ScaleTransitionPagerTitleView
import com.weme.live.view.indicator.bind
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView

class HomeNewIndicator @JvmOverloads constructor(
    context: Context,
    set: AttributeSet? = null,
) : MagicIndicator(context, set) {


    private var mViewPager: ViewPager2? = null

    private var labels: List<String>? = null

    fun attachViewPager(viewPager: ViewPager2, label: List<String>) {
        mViewPager = viewPager
        labels = label

        init(viewPager.context, viewPager)
        bind(mViewPager)
    }

    private fun init(context: Context, view_pager: ViewPager2) {
        val navigator = CommonNavigator(context)
      //  navigator.isAdjustMode = true
        navigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount() = labels?.size ?: 0

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val commonPagerTitleView = CommonPagerTitleView(context)
                val customLayout: View = LayoutInflater.from(context).inflate(R.layout.indicator_home_title, null)
                val tvTitle = customLayout.findViewById<TextView>(R.id.et_title)

                tvTitle.text = labels?.get(index) ?: ""
                commonPagerTitleView.setContentView(customLayout)
                val typefaceBook = ResourcesCompat.getFont(context, com.example.resources.R.font.book)
                val typefaceBold = ResourcesCompat.getFont(context, com.example.resources.R.font.bold)
                tvTitle.typeface = typefaceBold

                commonPagerTitleView.onPagerTitleChangeListener = object : CommonPagerTitleView.OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {

                    }

                    override fun onDeselected(index: Int, totalCount: Int) {

                    }

                    override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {
                        customLayout.alpha = 1.3f - leavePercent
                        tvTitle.typeface = typefaceBook

                    }

                    override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {
                        customLayout.alpha = (0.5f + enterPercent)

                        tvTitle.typeface = typefaceBold
                    }
                }
                commonPagerTitleView.setOnClickListener { view_pager.setCurrentItem(index) }
                return commonPagerTitleView
            }

            override fun getIndicator(p0: Context): IPagerIndicator? {
                val linePagerIndicator = LinePagerIndicator(context); //linePagerIndicator.roundRadius = context.dp2px(7).toFloat()
                linePagerIndicator.setColors(Color.parseColor("#21BEAB")) // linePagerIndicator.setBackgroundResource(R.drawable.bg_chat_notice_back)
                linePagerIndicator.mode = LinePagerIndicator.MODE_EXACTLY
                linePagerIndicator.lineHeight = context.dp2px(2).toFloat()
                return null
            }
        }
        setNavigator(navigator)
    }
}